var report1, report2;

$(function(){
	report1 = echarts.init(document.getElementById('report1'));
	report2 = echarts.init(document.getElementById('report2'));
	refresh();
	$("input[name='beginDateType']").change(function() {
		refreshProfit();
	});
	$("#profitPanel").panel({
		onResize : resizeEchartReport
	});
});

var hasUserSelect = false;
function userSelect(){
	hasUserSelect = true;
}

function changeAcount(newValue, oldValue) {
	if (oldValue && newValue) {
		frame.window.refresh({
			acountIds : newValue
		});
	}
}

function changeBeginDate() {
	if (!hasUserSelect) return;
	hasUserSelect = false;
	$(":radio[name='beginDateType']").prop("checked", false);
	refreshProfit();
}

function refresh(isRefresh) {
	$("#requestForm").form("clear");
	var acountId = $("#acountId").combobox("getValue");
	var detailColumn = $("#detailColumn").combobox("getValue");
	if(acountId<0){
		return;
	}
	$.ajax("./backreport.json", {
		data : {
			isRefresh : isRefresh,
			acountIds : acountId,
			quantDetailColumn : detailColumn
		},
		mask : true,
		success : function(data) {
			if (data.modelconfig) {
				$("#modelconfig").form("load", data.modelconfig);
				$("#risk").form("load", data.risk);
				loadProfitChart(data.profit);
				loadQuantDetail(data.quantDetail);
			}
		}
	});
}

function refreshProfit(){
	var data = $("#requestForm").form("getData");
	data.acountIds = $("#acountId").combobox("getValue");
	$.ajax("./profit.json",{
		mask:true,
		data:data,
		success:function(data) {
			if(data.profit){
				loadProfitChart(data.profit);
			}
		}
	});
}

function changeQuantDetailColumn(newValue){
	$.ajax("./quantdetail.json",{
		mask:true,
		data:{quantDetailColumn:newValue, acountIds : $("#acountId").combobox("getValue")},
		success:function(data) {
			if(data.quantDetail){
				loadQuantDetail(data.quantDetail);
			}
		}
	});
}

var lastWidth = 0;
function resizeEchartReport() {
	var containerWidth = $("#profitPanel").width();
	var width = containerWidth;
	if(containerWidth>1000){
		width = 1000;
	}else if(containerWidth<600){
		width = 600;
	}
	if(lastWidth == width){
		return;
	}
	var height = width/2;
	$("#report1").width(width);
	$("#report1").height(height);
	var myChart1 = echarts.getInstanceByDom(document.getElementById('report1'));
	if(myChart1) myChart1.resize();
	
	$("#report2").width(width);
	$("#report2").height(height);
	var myChart2 = echarts.getInstanceByDom(document.getElementById('report2'));
	if(myChart2) myChart2.resize();
};

function loadProfitChart(data){
	$("#requestForm").form("load", data.requestData);
	// 基于准备好的dom，初始化echarts实例
	var lines = data.lineData;
	var legend = [];
	var series = [];
	for ( var key in lines) {
		legend.push(key);
		series.push({
			name : key,
			type : 'line',
			data : lines[key]
		});
	}
	series[0].markPoint = {
		data : data.markPointData
	};
	// 指定图表的配置项和数据
	var option = {
		tooltip : {
			trigger : 'axis',
			axisPointer : {
				type : 'cross'
			},
			formatter : function(params) {
				var res = params[0].name;
				for (var i = 0, l = params.length; i < l; i++) {
					var a = '<span style="display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:' + params[i].color + '"></span>';
					res += '<br/>' + a + params[i].seriesName + ' : ' + params[i].value.toFixed(2) + "%";
				}
				return res;
			}
		},
		legend: {
			data:legend
		},
		dataZoom:{
			show: true,
			realtime: true,
			start: 0,
			end: 100
		},
		xAxis: {
		 	type: "category",
		    data: data.timeLine
		},
	    yAxis: [{
			name: '收益%',
			type: 'value',
	            }
	        ],
		series:series
	};
	report1.setOption(option);
}

function loadQuantDetail(data){
	// 基于准备好的dom，初始化echarts实例
	var lines = data.lineData;
	var legend = [];
	var series = [];
	for ( var key in lines) {
		legend.push(key);
		series.push({
			name : key,
			type : 'line',
			data : lines[key]
		});
	}
	// 指定图表的配置项和数据
	var option = {
		tooltip : {
	        trigger: 'axis',
	       	axisPointer : {
				type : 'cross'
			}
	    },
		legend: {
			data:legend
		},
		dataZoom:{
			show: true,
			realtime: true,
			start: 0,
			end: 100
		},
		xAxis: {
		 	type: "category",
		    data: data.timeLine
		},
	    yAxis: [{
			type: 'value',
	            }
	        ],
		series:series
	};
	report2.setOption(option);
}